const $ = require('../utils/utils')
const config = require('../config.js')


// 退押金
function returnDeposit() {
    return new Promise(function (resolve, reject) {
        wx.showModal({
            title: '退还押金',
            content: '押金确认退还后，账号将处于锁定状态，请确认是否还款押金。',
            confirmColor: "#FF3B30",
            cancelColor: "#007AFF",
            success: function (res) {
                if (res.confirm) {
                    let data = {
                        userid: wx.getStorageSync('userid'),
                        usertoken: wx.getStorageSync('usertoken')
                    }
                    let url = config.refundDeposit
                    $.post(url, data).then(data => {
                        console.log(data)
                        if (data.result == 1) {
                            $.showToast("退款成功")
                            reslove(data)
                        } else {
                            wx.showModal({
                                content: data.message,
                                showCancel: false,
                                confirmColor: "#FF3B30"
                            });
                            reject(data.message)
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                    reject()
                }
            }
        });
    })
}

function topupDeposit() {
    return new Promise(function (resolve, reject) {
        wx.showToast({
            title: '支付请求...',
            icon: 'loading',
            success: function (res) {
                let data = {
                    charge_code: "99",
                    charge_type: 0,
                    userid: wx.getStorageSync('userid'),
                    usertoken: wx.getStorageSync('usertoken'),
                    vip_type: 0
                }
                let url = config.paymentV2
                $.post(url, data).then(data => {
                    wx.hideLoading();
                    var payid = data.detail.prepayId;
                    var payargs = data.payargs;
                    if (data.code == '0') {
                        wx.requestPayment({
                            timeStamp: data.detail.timeStamp,
                            nonceStr: data.detail.nonceStr,
                            package: data.detail.pkg,
                            signType: data.detail.signType,
                            paySign: data.detail.sign,
                            'success': function (res) {
                                resolve(res)
                            },
                            'fail': function (res) {
                                //支付失败跳转页面
                                reject(res)
                            }
                        });
                    } else {
                        wx.showModal({
                            title: '支付错误！',
                            content: data.message,
                        })
                        reject()
                    }
                })
            }
        })
    })
}


// 充值
function topupAction(amount, chargeType) {
    return new Promise(function (resolve, reject) {
        wx.showToast({
            title: '支付请求...',
            icon: 'loading',
            success: function (res) {
                let data = {
                    chargeamount: amount,
                    chargetype: chargeType,
                    userid: wx.getStorageSync('userid'),
                    usertoken: wx.getStorageSync('usertoken'),
                    vip_type: 0
                }
                let url = config.paymentV2
                $.post(url, data).then(data => {
                    wx.hideLoading();
                    var payid = data.detail.prepayId;
                    var payargs = data.payargs;
                    if (data.code == '0') {
                        wx.requestPayment({
                            timeStamp: data.detail.timeStamp,
                            nonceStr: data.detail.nonceStr,
                            package: data.detail.pkg,
                            signType: data.detail.signType,
                            paySign: data.detail.sign,
                            'success': function (res) {
                                resolve(res)
                            },
                            'fail': function (res) {
                                //支付失败跳转页面
                                reject(res)
                            }
                        });
                    } else {
                        wx.showModal({
                            title: '支付错误！',
                            content: data.message,
                        })
                        reject()
                    }
                })
            }
        })
    })

}


// 优惠券计算金额
function calcPayAmount(params) {
    return new Promise(resolve => {
        wx.showLoading({
            title: '正在加载中...',
            mask: true
        })

        params.userid = wx.getStorageSync('userid')
        params.usertoken = wx.getStorageSync('usertoken')
        $.post(config.calcPayAmount, params).then(data => {
            wx.hideLoading()
            console.log('#vip_selCoupon_data--', data)
            resolve(data)
        }).catch(error => {
            wx.hideLoading()
        })
    })

}

function paymentV2(params) {

    return new Promise(function (resolve, reject) {
        $.showLoading("正在请求支付")

        params.userid = wx.getStorageSync('userid')
        params.usertoken = wx.getStorageSync('usertoken')

        let url = config.paymentV2
        $.post(url, params).then(data => {
            wx.hideLoading();
            var payid = data.detail.prepayId;
            var payargs = data.payargs;
            if (data.code == '0') {
                if (data.detail.payFlag == 1) {
                    wx.requestPayment({
                        timeStamp: data.detail.timeStamp,
                        nonceStr: data.detail.nonceStr,
                        package: data.detail.pkg,
                        signType: data.detail.signType,
                        paySign: data.detail.sign,
                        success: function (res) {
                            resolve(data)
                        },
                        fail: function (res) {
                            let data = {
                                userid: wx.getStorageSync('userid'),
                                usertoken: wx.getStorageSync('usertoken'),
                            }
                            let url = config.cancelPayment
                            $.post(url, data).then(data => {
                            })
                            //支付失败跳转页面
                            $.showToast('支付失败')
                            reject()
                        }
                    });
                } else {
                    resolve(data)
                }

            } else {
                wx.showModal({
                    title: '支付错误！',
                    content: data.message,
                })
                reject()
            }
        })
    })
}









module.exports = {
    topupAction: topupAction,
    returnDeposit: returnDeposit,
    topupDeposit: topupDeposit,
    calcPayAmount: calcPayAmount,
    paymentV2: paymentV2
}